'''验证工具'''

import jwt
from django.conf import settings
import time

def generate_token(data:dict):
    #增加过期时间数据
    data.update({'exp':settings.JWT_EXPIRE_TIME+time.time()})
    #通过加密算法加密
    token = jwt.encode(data,settings.JWT_SECRET_KEY,algorithm='HS256')
    return token

def verify_token(token):
    #解密过期会报错
    try:
        #通过解密算法解密
        data = jwt.decode(token,settings.JWT_SECRET_KEY,algorithms=['HS256'])
    except Exception:
        return None
    return data


if __name__=='__main__':
    data = {'phone':'123'}
    t = generate_token(data)
    print(t)
    d = verify_token(t)
    print(d)
